// src/pages/catering/shopComment/ShopComment.js
import getIn from '../../../../yali-sdk/utils/getIn';
import { SUCCESS } from '../../../../yali-sdk/utils/success';
import { FAIL } from '../../../../yali-sdk/utils/fail';
import { notify } from '../../../../yali-sdk/utils/NotifyRegistryCenter';
import shopCommend from '../../../../yali-sdk/business/catering/shopCommend';
import shopCommendDetail from '../../../../yali-sdk/business/catering/shopCommendDetail';
import { showFail, showSuccess, uploadImage, getImagePath } from '../../../config/common';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    feedbackText: '',
    submitParams: {
      pictures: [],
      content: '',
      feedback: 0,
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    const is_comment = typeof options.is_comment != 'number' ? Number(options.is_comment) : options.is_comment;
    if (is_comment) {
      this.commendDetail();
    }
    this.setData({ is_comment });
  },
  // 品论详情
  commendDetail() {
    const token = getIn(getApp(), ['globalData', 'token']);
    const shop_id = getIn(this.options, ['shop_id']);
    const params = { token, shop_id, order_id: Number(this.options.order_id) }
    global.ActionDispatcher.execute(shopCommendDetail, params, (code, res) => {
      if(code == SUCCESS) {
        const data = res.data;
        const submitParams = {
            pictures: data.pics,
            content: data.content,
            feedback: data.feedback,
          };
        this.setData({ submitParams, feedbackText: this.feedbackText(data.feedback) });
      }
    });
  },
  // 评论描述
  bindTextArea(e) {
    const value = e.detail.value;
    this.setData({ 'submitParams.content': value })
  },
  // 上传图片
  updatePic() {
    const token = getIn(getApp(), ['globalData', 'token']);
    wx.chooseImage({
      count: 5, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: (res) => {
        let pictures = [...this.data.submitParams.pictures];
        if (this.data.submitParams.pictures.length < 5) {
          getIn(res, ['tempFilePaths']) && getIn(res, ['tempFilePaths']).map &&
            getIn(res, ['tempFilePaths']).map((o) => {
            // 先用loading图片替代
            pictures.push('/image/loading.gif');
            // 获取当前图片索引
            const index = pictures.length - 1;
            // setData函数为异步函数，先确保保存一个loading图片替换一个已加载图片
            this.setData({ 'submitParams.pictures': pictures }, () => {
                uploadImage && uploadImage(o, 'WMS_MP', token, (status, data) => {
                  if (status == SUCCESS) {
                    // const m_image = getImagePath(getIn(data, ['data', 'url']));
                    const m_image = data[0].url;
                    // 替换之前loading图片
                    pictures.splice(index, 1, m_image);
                    this.setData({ 'submitParams.pictures': pictures })
                  } else if (status == FAIL) {
                    showFail('上传失败');
                  }
                });
              });
            });
        } else {
          showFail && showFail('图片已达上限')
        }
      }
    })
  },
  // 预览图片
  previewImg (e) {
    //获取当前图片的下标
    const index = e.currentTarget.dataset.index;
    wx.previewImage({
      //当前显示图片
      current: this.data.submitParams.pictures[index],
      //所有图片
      urls: this.data.submitParams.pictures
    })
  },
  // 删除图片
  deleteImg (e) {
    var pictures = this.data.submitParams.pictures;
    var index = e.currentTarget.dataset.index;
    pictures.splice(index, 1);
    this.setData({
      'submitParams.pictures': pictures
    });
  },
  // 取消
  cancel() {
    global.navigateHelper && global.navigateHelper.navigateBack();
  },
  // 星级评价回调函数
  selectCallback(e) {
    const feedback = getIn(e.detail, ['feedback']);
    this.setData({ 'submitParams.feedback': feedback, feedbackText: this.feedbackText(feedback) });
  },
  // 星星数量描述
  feedbackText(feedback) {
    let feedbackText = '';
    feedback = typeof feedback != 'number' ? Number(feedback) : feedback;
    switch (Math.ceil(feedback)) {
      case 1: feedbackText = '非常差';
        break;
      case 2: feedbackText = '差';
        break;
      case 3: feedbackText = '一般';
        break;
      case 4: feedbackText = '好';
        break;
      case 5: feedbackText = '非常好';
        break;
    }
    return feedbackText
  },
  // 发表评论
  submit() {
    const token = getIn(getApp(), ['globalData', 'token']);
    const shop_id = getIn(this.options, ['shop_id']);
    const params = { token, shop_id, order_id: Number(this.options.order_id), ...this.data.submitParams }
    if (params.feedback == 0) {
      showFail('请对店铺评分')
    } else {
      
      global.ActionDispatcher.execute(shopCommend, params, (code, res) => {
        if (code == SUCCESS) {
          notify('updateOrderList');
          showSuccess('操作成功');
          global.navigateHelper.navigateBack();
        } else if (code == FAIL) {
          showFail(res.msg);
          global.navigateHelper.navigateBack();
        }
      });
    }
    
  }
})